package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.dao.third.JuheCarMapper;
import com.zjdiepu.www.ibiz.third.IJuheCarService;
import com.zjdiepu.www.model.third.dto.JuheCar;
import com.zjdiepu.www.model.third.ext.JuheCarExt;
import com.zjdiepu.www.model.third.request.CarModelRequest;
import com.zjdiepu.www.model.third.response.CarModelResponse;

/**
 * 车型Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年03月23日 20时33分<br/>
 * @since 1.0<br/>
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED, timeout = 60)
public class JuheCarServiceImpl extends BaseService implements IJuheCarService {
	public static final Logger logger = LoggerFactory.getLogger(JuheCarServiceImpl.class);

	@Autowired
	private JuheCarMapper juheCarMapper;
	
	@Override
	public String selectNameByCarId(String carId, String brandName, String seriesName) {
		JuheCar juheCar = new JuheCar();
		juheCar.setDpStatus(DpStatus.NORMAL.getCode());
		juheCar.setCarId(carId);
		juheCar = juheCarMapper.selectRefined(juheCar);
		if(null == juheCar) {
			return null;
		}
		String carName = juheCar.getCxname();
		if(StringUtils.isBlank(carName)) {
			return null;
		}
		carName = carName.trim();
		if(StringUtils.isNotBlank(brandName) && carName.startsWith(brandName)) {
			carName = carName.substring(brandName.length() + 1);
		}
		if(StringUtils.isNotBlank(seriesName) && carName.startsWith(seriesName)) {
			carName = carName.substring(seriesName.length() + 1);
		}
		return carName;
	}
	
	@Override
	public int selectCarId() {
		int maxCarId = juheCarMapper.selectMaxCarId();
		if(maxCarId > 10000000) {
			return maxCarId + 1;
		} else {
			return 10000001;
		}
	}
	
	@Override
	public int countMuliByExt(CarModelRequest carModelRequest) {
		return juheCarMapper.countMuliByExt(carModelRequest);
	}
	
	@Override
	public List<CarModelResponse> selectMuliByExt(CarModelRequest carModelRequest) {
		return juheCarMapper.selectMuliByExt(carModelRequest);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void saveUpdateAndDelete(JuheCar juheCar, OperateType operType) {
		switch (operType) {
		case add:
			juheCar.initInsertData(juheCar.getModifier(),juheCar.getModifierName());
			juheCarMapper.insert(juheCar);
			break;
		case edit:
			juheCar.initUpdateData(juheCar.getModifier(),juheCar.getModifierName());
			// 影响的记录数
			int updateCount = juheCarMapper.updateByPrimaryKeySelective(juheCar);
			if(updateCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(updateCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		case delete:
			juheCar.initDeleteData(juheCar.getModifier(),juheCar.getModifierName());
			// 影响的记录数
			int deleteCount = juheCarMapper.updateByPrimaryKeySelective(juheCar);
			if(deleteCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(deleteCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheCar> selectByExt(JuheCarExt juheCarExt) {
		return juheCarMapper.selectByExt(juheCarExt);
	}
	
	@Override
	public int countByExt(JuheCarExt juheCarExt){
		return juheCarMapper.countByExt(juheCarExt);
	}
	
	@Override
	public JuheCar selectById(String id) {
		return juheCarMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheCarMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(JuheCar juheCar) {
		return juheCarMapper.countRefined(juheCar);
	}

	@Override
	public JuheCar selectRefined(JuheCar juheCar) {
		return juheCarMapper.selectRefined(juheCar);
	}
	

	@Override
	public boolean repeat(JuheCar juheCar) {
		int count = countRefined(juheCar);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(juheCar.getId())) {
			return true;
		}
		JuheCar dbItem = selectRefined(juheCar);
		if(null == dbItem) {
			return false;
		}
		return !juheCar.getId().equals(dbItem.getId());
	}
}
